home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / smallt~1 / smallt~1.zoo / String.st < prev    next >
Encoding:
Text File  |  1990-05-26  |  6.0 KB  |  243 lines

  1. "======================================================================
  2. |
  3. |   String Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1988, 1989, 1990 Free Software Foundation, Inc.
  11. | Written by Steve Byrne.
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change 
  33. | sbyrne      3 Sep 89      added asString for method source package
  34. |
  35. | sbyrne     25 Apr 89      created.
  36. |
  37. "
  38.  
  39. ArrayedCollection variableByteSubclass: #String
  40.           instanceVariableNames: ''
  41.           classVariableNames: ''
  42.           poolDictionaries: ''
  43.           category: nil.
  44.  
  45. String comment: 
  46. 'My instances represent string data types.  I provide accessing and
  47. manipulation methods for string data types.' !
  48.  
  49. !String class methodsFor: 'basic'!
  50.  
  51. fromString: aString
  52.     " ### probably should use regular copy"
  53.     ^aString copyFrom: 1 to: aString size
  54. !
  55.  
  56. readFrom: aStream
  57.     | str newChar |
  58.     str _ WriteStream on: (String new: 0).
  59.     aStream do:            " ### Is this risky, doing a next inside? "
  60.         [ :char | char == $'
  61.                   ifTrue: [ newChar _ aStream next.
  62.                           newChar == $' ifFalse: [ str nextPut: $' ].
  63.                 str nextPut: newChar ]
  64.                       ifFalse: [ str nextPut: char ]
  65.     ].
  66.     ^str contents
  67. !!
  68.  
  69.  
  70.  
  71. !String methodsFor: 'comparing'!
  72.  
  73. < aString
  74.     "Return true if the receiver is less than aString, ignoring case
  75.     differences."
  76.     self >= aString ifTrue: [ ^false ]
  77.                     ifFalse: [ ^true ]
  78. !
  79.  
  80. > aString
  81.     "Return true if the receiver is greater than aString, ignoring case
  82.     differences."
  83.     self <= aString ifTrue: [ ^false ]
  84.                     ifFalse: [ ^true ]
  85. !
  86.  
  87. <= aString
  88.     "Returns true if the receiver is less than or equal to aString,
  89.     ignoring case differences.  If is receiver is an initial substring of
  90.     aString, it is considered to be less than aString."
  91.     | c1 c2 |
  92.     " Scan self and aString until a character is clearly greater or lesser
  93.       (All preceding characters must have been equal).  If the end is reached,
  94.       one of the strings is a possibly improper initial substring of the other,
  95.       and for the receiver to be less than aString, it must be the initial
  96.       substring."
  97.     1 to: (self size min: aString size) do:
  98.         [ :i | c1 _ (self at: i) asLowercase.
  99.            c2 _ (aString at: i) asLowercase.
  100.            c1 < c2 ifTrue: [ ^true ].
  101.            c1 > c2 ifTrue: [ ^false ] ].
  102.     ^self size <= aString size
  103. !
  104.  
  105. >= aString
  106.     "Returns true if the receiver is greater than or equal to aString,
  107.     ignoring case differences.  If is aString is an initial substring of
  108.     the receiver, it is considered to be less than the receiver."
  109.     | c1 c2 |
  110.     1 to: (self size min: aString size) do:
  111.         [ :i | c1 _ (self at: i) asLowercase.
  112.            c2 _ (aString at: i) asLowercase.
  113.            c1 < c2 ifTrue: [ ^false ].
  114.            c1 > c2 ifTrue: [ ^true ] ].
  115.     ^self size >= aString size
  116. !
  117.  
  118.  
  119.  
  120. sameAs: aString
  121.     "Returns true if the receiver is the same string as aString, ignoring
  122.     case differences."
  123.     self size ~= aString size ifTrue: [ ^false ].
  124.     1 to: self size do:
  125.         [ :i | (self at: i) asLowercase ~= (aString at: i) asLowercase
  126.             ifTrue: [ ^false ] ].
  127.     ^true
  128. !
  129.  
  130.  
  131. match: aString
  132.     ^self asLowercase matchSubstring: 1 in: aString asLowercase at: 1
  133. !!
  134.  
  135.  
  136.  
  137. !String methodsFor: 'converting'!
  138.  
  139. asUppercase
  140.     "Returns a copy of self as an uppercase string"
  141.     | newStr |
  142.     newStr _ self species new: self size.
  143.     1 to: self size do:
  144.         [ :i | newStr at: i put: (self at: i) asUppercase ].
  145.     ^newStr
  146. !
  147.  
  148. asLowercase
  149.     "Returns a copy of self as a lowercase string"
  150.     | newStr |
  151.     newStr _ self species new: self size.
  152.     1 to: self size do:
  153.         [ :i | newStr at: i put: (self at: i) asLowercase ].
  154.     ^newStr
  155. !
  156.  
  157. asString
  158.     "But I already am a string!  Really!"
  159.     ^self
  160. !
  161.  
  162. fileName
  163.     "But I don't HAVE a file name!"
  164.     ^nil
  165. !
  166.  
  167. fileName
  168.     "But I don't HAVE a file position!"
  169.     ^nil
  170. !
  171.  
  172. asSymbol
  173.     "Returns the symbol corresponding to the string"
  174.     ^Symbol intern: self
  175. !!
  176.  
  177.  
  178.  
  179. !String methodsFor: 'copying'!
  180.  
  181. shallowCopy
  182.     | newStr |
  183.     newStr _ self species new: self size.
  184.     1 to: self size do:
  185.         [ :i | newStr at: i put: (self at: i) ].
  186.     ^newStr
  187. !
  188.  
  189. deepCopy
  190.     ^self shallowCopy
  191. !!
  192.  
  193.  
  194.  
  195.  
  196. !String methodsFor: 'printing'!
  197.  
  198. printOn: aStream
  199.     ^self do: 
  200.     [ :char | aStream nextPut: char ] "this seems too primitive"
  201. !!
  202.  
  203.  
  204.  
  205. !String methodsFor: 'storing'!
  206.  
  207. storeOn: aStream
  208.     aStream nextPut: $'.
  209.     self do:
  210.         [ :char | char == $' ifTrue: [ aStream nextPut: char ].
  211.               aStream nextPut: char ].
  212.     aStream nextPut: $'
  213. !!
  214.  
  215.  
  216.  
  217. !String methodsFor: 'private'!
  218.  
  219. matchSubstring: p in: aString at: s
  220.     | pc |
  221.     p > self size
  222.         ifTrue: [ ^s > aString size ].
  223.     pc _ self at: p.
  224.     pc = $*
  225.         ifTrue: [ s to: (aString size) + 1 do:
  226.                 [ :ss | (self matchSubstring: p + 1
  227.                           in: aString
  228.                   at: ss)
  229.                     ifTrue: [ ^true ] ].
  230.           ^false ].
  231.     s > aString size ifTrue: [ ^false ].
  232.     pc = $#
  233.         ifTrue: [ ^self matchSubstring: p + 1 in: aString at: s + 1 ].
  234.  
  235.     pc = (aString at: s)
  236.         ifTrue: [ ^self matchSubstring: p + 1 in: aString at: s + 1 ].
  237.     ^false
  238. !!
  239.  
  240.  
  241.